﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;
using System.Text;

namespace HRProject.Code.DAO
{
    public class DownloadableFileDAO : BaseDAO
    {
        public DownloadableFileDAO()
            : base ()
        {
        }

        public BLDownloadableFile Get(string generatedName)
        {
            if (generatedName == null)
            {
                return null;
            }
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@generatedName", generatedName, ParameterDirection.Input));
            DataTable data = select("usp_GetDownloadableFile", condition);
            if (data.Rows.Count > 0)
            {
                BLDownloadableFile result = new BLDownloadableFile();
                result.GeneratedName = (string)data.Rows[0]["GeneratedName"];
                result.FileName = (string)data.Rows[0]["FileName"];
                result.CreatedTime = (DateTime)data.Rows[0]["CreatedTime"];
                result.Url = (string)data.Rows[0]["Url"];
                return result;
            }
            return null;
        }

        public int Add(BLDownloadableFile file, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@generatedName", file.GeneratedName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fileName", file.FileName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@url", file.Url, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_AddDownloadableFile", condition);
            errorCode = (int)condition[condition.Count-1].ParamValue;
            return result;
        }

        public int Delete(string generatedName, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@generatedName", generatedName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteDownloadableFile", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }
    }
}
